package org.yourscrum.web.story;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yourscrum.domain.Story;
import org.yourscrum.service.StoryService;
import org.yourscrum.web.InitBinderForGoogle;

import java.util.List;

/**
 * @author Jettro Coenradie
 */
@Controller
@RequestMapping("/story/**")
public class StoryController extends InitBinderForGoogle {
    protected static final String VIEW_STORY_SHOW = "story/show";
    protected static final String VIEW_STORY_LIST = "story/stories";
    protected static final String VIEW_STORY_FORM = "story/edit";
    protected static final String REDIRECT_STORY_LIST = "redirect:/story";
    protected static final String VIEW_STORY_TASK_SHOW = "story/task/show";
    protected static final String VIEW_STORY_TASK_FORM = "story/task/form";

    private StoryService storyService;

    @Autowired
    public StoryController(StoryService storyService) {
        this.storyService = storyService;
    }

    /* All story related methods */

    @RequestMapping(value = "/story", method = RequestMethod.GET)
    public String list(ModelMap modelMap) {
        List<Story> stories = storyService.listAllStories();
        modelMap.addAttribute("stories", stories);
        return VIEW_STORY_LIST;
    }

    @RequestMapping(value = "/story/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {
        Assert.notNull(id, "Identifier must be provided.");
        modelMap.addAttribute("story", storyService.obtainStoryById(id));
        return VIEW_STORY_SHOW;
    }

    @RequestMapping(value = "/story/form", method = RequestMethod.GET)
    public String form(ModelMap modelMap) {
        modelMap.addAttribute("story", new StoryVO());
        return VIEW_STORY_FORM;
    }

    @RequestMapping(value = "/story/{id}/form", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, ModelMap modelMap) {
        Assert.notNull(id, "Identifier must be provided.");
        Story existingStory = storyService.obtainStoryById(id);
        StoryVO story = new StoryVO(existingStory.getId(), existingStory.getName(), existingStory.getDescription());
        modelMap.addAttribute("story", story);
        return VIEW_STORY_FORM;
    }


    @RequestMapping(value = "/story", method = RequestMethod.POST)
    public String createOrUpdate(@ModelAttribute("story") StoryVO story, BindingResult result) {
        Assert.notNull(story, "A story instance must be provided");
        new StoryValidator().validate(story, result);

        if (result.hasErrors()) {
            return VIEW_STORY_FORM;
        } else {
            if (story.getId() == null) {
                this.storyService.createStory(story.getName(), story.getDescription());
            } else {
                this.storyService.changeStory(story.getId(), story.getName(), story.getDescription());
            }
            return REDIRECT_STORY_LIST;
        }
    }

    @RequestMapping(value = "/story/reorder", method = RequestMethod.POST)
    public String reorderStories(@ModelAttribute("ids") String ids,BindingResult result) {

        return "hello";
    }

}
